<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>

    // 递归(函数在执行过程中调用了自己)  

    // 特征: 先传递再回归

    // 注意:
    // 递归需要有跳出条件(临界值), 否则会形成死递归

    // 求1-5的和

    // sum(5) = 1 + 2 + 3 + 4 + 5;   sum(5) = 5 + sum(4)
    // sum(4) = 1 + 2 + 3 + 4;       sum(4) = 4 + sum(3)
    // sum(3) = 1 + 2 + 3;           sum(3) = 3 + sum(2)
    // sum(2) = 1 + 2 ;              sum(2) = 2 + sum(1)
    // sum(1) = 1;                   sum(1) = 1;

    // 依赖性递归 (当前值的结果依赖于前一个/前多个的结果)
    debugger;
    function sum(n){// 5
        if(n == 1) return 1;
        return n + sum(n-1);
    }

    // sum(5) =>  5 + sum(4) => 4 + sum(3) => 3 + sum(2) => 2 + sum(1)
    //                                                           ||
    //   15   <=       10   <=         6   <=       3   <=       1

    var res = sum(5);
    console.log(res);


</script>
</html>